{extend name="public:base"}
{block name='css'}
{/block}

{block name='body'}
<div class="view view-main white-navbar white-page white-toolbar">
    <div data-name="home" class="page">

        <div class="navbar">
            <div class="navbar-inner">
                <div class="left"><a href="{$my_prev_url|default='/'}" class="external"><i class="iconfont icon-fanhui"></i></a></div>
                <div class="center"><span>修改手机号</span></div>
                <div class="right"></div>
            </div>
        </div>

        <div class="page-content padding-bottom50 ">

            <div class="edit-tel row">
                <div class="col-80">
                    <form name="form" action="" id="saveTel" method="post" >
                        <div class="row b">
                            <i class="iconfont icon-shouji"></i>
                            <div class="input">
                                <input type="number" name="tel" placeholder="请输入手机号..." />
                            </div>
                        </div>
                        <div class="row b">
                            <i class="iconfont icon-yanzhengma"></i>
                            <div class="input">
                                <input type="text" name="code" placeholder="请输入验证码..." />
                            </div>
                            <span class="getCode" onclick="getCode();">获取验证码</span>
                        </div>
                        <p class="row segmented-round">
                            <button onclick="" class="col button button-big button-fill" style="background:#66ba61;">保存修改</button>
                        </p>
                    </form>
                </div>
            </div>

        </div>



    </div>
</div>
{/block}

{block name="js"}
<script type="text/javascript">
    var counttime = 60;
    //获取验证码
    function getCode(){
        if ( counttime != 60 ) {
            return false ;
        }

        var tel = $("input[name='tel']").val() ;

        if ( ! isTel( tel ) ) {
            messageToast("请输入正确的手机号");
            return false ;
        }

        $.post( "{:url('index/sendCode')}" , {tel:tel,state:1}, function ( data ) {
            if ( data.status == 'y' ) {
                //ajax后才执行
                settime();
            }
            messageToast( data.info ) ;
        }, "json");

    }

    //获取验证码倒计时
    function settime() {
        if ( counttime == 0 ) {
            $('.getCode').text("获取验证码");
            counttime = 60;
            return false;
        } else {
            $('.getCode').text('重新获取('+counttime+'s)');
            counttime--;
        }
        setTimeout(function () {
            settime();
        },1000);
    }

    var ajaxOn = false ;
    $().ready(function () {
        $("#saveTel").validate({
            rules: {
                tel: "required",
                code: "required"
            },
            messages: {
                tel: '请输入手机号',
                code: '请输入验证码'
            },
            showErrors: function (errorMap, errorList) {
                $.each(errorList, function (i, v) {
                    messageToast( v.message );
                    return false;
                });
            },
            onfocusout: false,
            onsubmit : true ,
            onkeyup : false ,
            onclick : false ,
            focusInvalid:false,
            submitHandler:function(form){
                if (ajaxOn) {
                    return false ;
                }
                ajaxOn = true ;
                $.post("", $("#saveTel").serialize(), function(data){
                    messageToast( data.info );
                    if (data.status == 'y') {
                        setTimeout(function(){
                            self.location = "{:url('info')}";
                        },1000);
                    }
                    ajaxOn = false ;
                }, 'json');

                return false ;

            }
        });
    });
</script>
{/block}